/**
 * 饲料和饲料添加剂生产企业检查记录表
 * @constructor
 */
function FeedProduction() {
}

FeedProduction.init = function () {
    getName1();
    $('#tableList').bootstrapTable({
        url: '/feedProduction/tableSearch',
        method: 'post',
        striped: true, //是否显示行间隔色
        toolbar: "#toolbar",
        sidePagination: "true",
        dataField: "data",//这是返回的json数组的key.默认是"rows".这里只有前后端约定好就行
        showToggle: true, //是否显示详细视图和列表视图的切换按钮
        striped: true, // 是否显示行间隔色
//search : "true",
        uniqueId: "id",
        pageList: [5, 10, 20, 50, 100],//可选择单页记录数
        pageSize: "10",
        pagination: true, // 是否分页
        sortable: true, // 是否启用排序
        sidePagination: 'server', // 设置为服务器端分页     客户端：client
        search: false,
        showColumns: true,
        showRefresh: true,
        showExport: true,
//得到查询的参数
        queryParams: function (params) {
//这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var query_params = {
//********table的参数 不变
                pageSize: params.limit,                         //页面大小
                pageIndex: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order, //排位命令（desc，asc）
//********
//获取查询的对象，往下加入
                timeInterval: $('#search_InspectionDate').val(), // 请求时向服务端传递的参数
            };
            return query_params;
        },
        columns: [
            {
//field: 'Number',//可不加
                title: '序号',//标题  可不加
                align: "center",
                width: 40,
                formatter: function (value, row, index) {
                    var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                    var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                    return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                }
            },
            {field: 'enterpriseName', title: '企业名称', align: 'center'},
            {field: 'certificateNo', title: '证号', align: 'center'},
            {field: 'address', title: '生产地址', align: 'center'},
            {field: 'legalPerson', title: '法定代表人', align: 'center'},
            {field: 'productCategory', title: '产品类别', align: 'center'},
            {field: 'phone', title: '联系电话', align: 'center'},
            {field: 'yearNumber', title: '年产量', align: 'center'},
            {field: 'enterpriseNumber', title: '企业人数', align: 'center'},
            {field: 'drugUse', title: '用药情况', align: 'center'},
            {field: 'equipment', title: '生产及检化验设备情况', align: 'center'},
            {field: 'recordSituation', title: '各项记录情况', align: 'center'},
            {field: 'workSituation', title: '持证上岗情况', align: 'center'},
            {field: 'labelSituation', title: '标签与包装情况', align: 'center'},
            {field: 'otherSituation', title: '其它情况', align: 'center'},
            {field: 'rectification', title: '需要整改项目', align: 'center'},
            {field: 'sign', title: '企业代表签字', align: 'center'},
            {field: 'dayLimit', title: '整改日限', align: 'center'},
            {field: 'enforcementNo', title: '检查人员及执法证件号', align: 'center'},
            {field: 'department', title: '检查部门', align: 'center'},
            {field: 'departmentPhone', title: '检查部门电话', align: 'center'},
            {field: 'inspectionDate', title: '检查日期', align: 'center'},
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    optionHtml = optionHtml + "<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"edit\")' title=\"修改\" ><i class=\"fa fa-edit\"></i>修改</a>";
                    optionHtml = optionHtml + " <a class=\"btn btn-danger btn-xs\" href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"delete\")'  title=\"删除\"><i class=\"fa fa-remove\"></i>删除</a>"
                    return optionHtml;

                }
            }
        ]
    });
}

laydate.render({
    elem: '#text_inspectionDate',
    theme: 'molv',
    type:"date",
});
laydate.render({
    elem: '#search_InspectionDate',
    range: true,
});
//查询方法****************************
function doSearch() {
// $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
    $("#tableList").bootstrapTable("refreshOptions", {pageNumber: 1});
}

//重置****************************
function doRest() {
    $("#search_InspectionDate").val("");
// $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
    $("#tableList").bootstrapTable("refreshOptions", {pageNumber: 1});
}

//增加方法****************************
var Action = "";
var id = 0;
var pxt = "/feedProduction"; //控制器名称
function addRecord() {
    id = 0;
    Action = "add";
    $("#myform")[0].reset();

}

//增加/修改数据保存****************************
function saveRecord() {
    if (!validateForm()) return false;
//创建传递的参数
    var postdata = {
        id: id,
        action: Action,

        enterpriseName: $("#text_enterpriseName").find("option:selected").text(),
        enterpriseId: $("#text_enterpriseName").val(),
        certificateNo: $("#text_certificateNo").val(),
        address: $("#text_address").val(),
        legalPerson: $("#text_legalPerson").val(),
        productCategory: $("#text_productCategory").val(),
        phone: $("#text_phone").val(),
        yearNumber: $("#text_yearNumber").val(),
        enterpriseNumber: $("#text_enterpriseNumber").val(),
        drugUse: $("#text_drugUse").val(),
        equipment: $("#text_equipment").val(),
        recordSituation: $("#text_recordSituation").val(),
        workSituation: $("#text_workSituation").val(),
        labelSituation: $("#text_labelSituation").val(),
        otherSituation: $("#text_otherSituation").val(),
        rectification: $("#text_rectification").val(),
        sign: $("#text_sign").val(),
        dayLimit: $("#text_dayLimit").val(),
        enforcementNo: $("#text_enforcementNo").val(),
        department: $("#text_department").val(),
        departmentPhone: $("#text_departmentPhone").val(),
        inspectionDate: $("#text_inspectionDate").val(),
    };
// console.log(JSON.stringify(postdata));
//发送异步请求到后台保存用户数据
    $.ajax({
        url: pxt + (Action == "add" ? "/save" : "/update"),
        type: "post",
        data: JSON.stringify(postdata),
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            if (data.code === "200") {
                $('#myModalAdd').modal('hide')
                doSearch();
                swal("操作成功!", "请继续操作", "success");
            } else {
                swal("操作失败!", "请重试", "error");
            }
        }
    });
}

//操作相关方法****************************
function actionRecord(data, type) {
    if (type == "edit") {
//修改
        id = data.id;
        Action = "edit";
        $("#myform")[0].reset();
        $("#save").removeAttr("disabled");
        WriteData(data)
        $('#myModalAdd').modal('show')
    } else {
//删除
        swal({
            title: "确认是否删除",
            text: "是否确认要删除该条记录？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
//点击删除之后此处进行传递后台
            $.ajax({
                type: "delete",
                url: pxt + "/deleteById/" + data.id,
                dataType: "json",
                contentType: "application/json",
                success: function (data, status) {
                    if (data.code == "200") {
                        doSearch();
                        swal("删除成功!", "请继续操作", "success");
                    } else {
                        swal("删除失败!", "请重试", "error");
                    }
                }
            });

        });
    }
}

//给控件赋值****************************
function WriteData(data) {
    $("#text_enterpriseName").val(data.enterpriseId);
    $("#text_certificateNo").val(data.certificateNo);
    $("#text_address").val(data.address);
    $("#text_legalPerson").val(data.legalPerson);
    $("#text_productCategory").val(data.productCategory);
    $("#text_phone").val(data.phone);
    $("#text_yearNumber").val(data.yearNumber);
    $("#text_enterpriseNumber").val(data.enterpriseNumber);
    $("#text_drugUse").val(data.drugUse);
    $("#text_equipment").val(data.equipment);
    $("#text_recordSituation").val(data.recordSituation);
    $("#text_workSituation").val(data.workSituation);
    $("#text_labelSituation").val(data.labelSituation);
    $("#text_otherSituation").val(data.otherSituation);
    $("#text_rectification").val(data.rectification);
    $("#text_sign").val(data.sign);
    $("#text_dayLimit").val(data.dayLimit);
    $("#text_enforcementNo").val(data.enforcementNo);
    $("#text_department").val(data.department);
    $("#text_departmentPhone").val(data.departmentPhone);
    $("#text_inspectionDate").val(data.inspectionDate);
}

//表单验证*****************************
function validateForm() {
//validate方法参数可选
    return $("#myform").validate({
        rules: {

            text_enterpriseName: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_certificateNo: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_address: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_legalPerson: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_productCategory: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_phone: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_yearNumber: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_enterpriseNumber: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_drugUse: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_equipment: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_recordSituation: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_workSituation: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_labelSituation: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_otherSituation: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_rectification: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_sign: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_dayLimit: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_enforcementNo: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_department: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_departmentPhone: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionDate: {
                required: true,//必填
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
        },
//错误信息提示
        messages: {
            text_enterpriseName: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_certificateNo: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_address: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_legalPerson: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_productCategory: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_phone: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_yearNumber: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_enterpriseNumber: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_drugUse: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_equipment: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_recordSituation: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_workSituation: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_labelSituation: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_otherSituation: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_rectification: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_sign: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_dayLimit: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_enforcementNo: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_department: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_departmentPhone: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
            text_inspectionDate: {
                required: "(必填)",
                minlength: 1, //最少1个字符
                maxlength: 50,//最多50个字符
                // number: true, //必须是数字
            },
        },
    }).form();

}
//给下拉选赋值
function getName1() {
    var postdata={
        types:2,
    };
    $.ajax({
        url: "/mechanism/getName",
        type:"post",
        data: JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            $('#text_enterpriseName').empty();
            $('#text_enterpriseName').append("<option value='-1'>请选择企业名称</option>");
            $.each(data.data,function (index,item) {
                $('#text_enterpriseName').append("<option value='"+item.id+"'>"+item.name+"</option>");
            })
        }
    });
}
//根据下拉选自动生成表格内容
function getName2(a) {
    console.log("进入")
    var postdata={
        types:2,
        id:a,
    };
    $.ajax({
        url: "/mechanism/getName",
        type:"post",
        data: JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            if (data.data.length <= 0){
                $('#text_certificateNo').val("");
                $('#text_legalPerson').val("");
                $('#text_address').val("");
                $('#text_phone').val("");
                return false;
            }
            $.each(data.data,function (index,item) {
                $('#text_certificateNo').val(item.idCard);
                $('#text_legalPerson').val(item.userName);
                $('#text_address').val(item.fullAddress);
                $('#text_phone').val(item.phone);
            })
        }
    });
}